<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="订单ID" prop="orderId">
        <el-input v-model="dataForm.orderId" placeholder="订单ID"></el-input>
      </el-form-item>
      <el-form-item label="商品ID" prop="produceId">
        <el-input v-model="dataForm.produceId" placeholder="商品ID"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="produceName">
        <el-input v-model="dataForm.produceName" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="购买时单价" prop="producePrice">
        <el-input v-model="dataForm.producePrice" placeholder="购买时单价"></el-input>
      </el-form-item>
      <el-form-item label="购买数量" prop="produceQuantity">
        <el-input v-model="dataForm.produceQuantity" placeholder="购买数量"></el-input>
      </el-form-item>
      <el-form-item label="商品封面图" prop="produceIcon">
        <!-- <el-input v-model="dataForm.produceIcon" placeholder="商品封面图"></el-input> -->
        <single-upload v-model="dataForm.produceIcon"></single-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        orderDetailId: 0,
        orderId: "",
        produceId: "",
        produceName: "",
        producePrice: "",
        produceQuantity: "",
        produceIcon: "",
      },
      dataRule: {
        orderId: [
          { required: true, message: "订单ID不能为空", trigger: "blur" },
        ],
        produceId: [
          { required: true, message: "商品ID不能为空", trigger: "blur" },
        ],
        produceName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        producePrice: [
          { required: true, message: "购买时单价不能为空", trigger: "blur" },
        ],
        produceQuantity: [
          { required: true, message: "购买数量不能为空", trigger: "blur" },
        ],
        produceIcon: [
          { required: true, message: "商品封面图不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.orderDetailId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.orderDetailId) {
          this.$http({
            url: this.$http.adornUrl(
              `/order/orderdetail/info/${this.dataForm.orderDetailId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.orderId = data.orderDetail.orderId;
              this.dataForm.produceId = data.orderDetail.produceId;
              this.dataForm.produceName = data.orderDetail.produceName;
              this.dataForm.producePrice = data.orderDetail.producePrice;
              this.dataForm.produceQuantity = data.orderDetail.produceQuantity;
              this.dataForm.produceIcon = data.orderDetail.produceIcon;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/order/orderdetail/${
                !this.dataForm.orderDetailId ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              orderDetailId: this.dataForm.orderDetailId || undefined,
              orderId: this.dataForm.orderId,
              produceId: this.dataForm.produceId,
              produceName: this.dataForm.produceName,
              producePrice: this.dataForm.producePrice,
              produceQuantity: this.dataForm.produceQuantity,
              produceIcon: this.dataForm.produceIcon,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
